﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace OF.Utility
{
    public class DataBindHelper
    {
        /// <summary>
        /// 绑定下拉框数据到一个泛型实例列表中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="drpList"></param>
        /// <param name="list"></param>
        /// <param name="textField"></param>
        /// <param name="valueField"></param>
        /// <param name="AppendDataBoundItems"></param>
        public static void SetDropDownDataSource<T>(DropDownList drpList, IList<T> list, string textField, string valueField, bool AppendDataBoundItems = true)
        {

            drpList.AppendDataBoundItems = AppendDataBoundItems;
            var nCount = drpList.Items.Count;
            for (int i = 0; i < nCount; i++)
            {
                if (i > 0)
                {
                    drpList.Items.RemoveAt(1);
                }
            }
          
            drpList.DataTextField = textField;
            drpList.DataValueField = valueField;
            drpList.DataSource = list;
            drpList.DataBind();
        }
        /// <summary>
        /// 绑定下拉框数据到一个泛型实例列表中
        /// </summary>
        /// <param name="drpList"></param>
        /// <param name="list">DataTable</param>
        /// <param name="textField"></param>
        /// <param name="valueField"></param>
        /// <param name="AppendDataBoundItems"></param>
        public static void SetDropDownDataSource(DropDownList drpList, DataTable list, string textField, string valueField, bool AppendDataBoundItems = true)
        {

            drpList.AppendDataBoundItems = AppendDataBoundItems;
            var nCount = drpList.Items.Count;
            for (int i = 0; i < nCount; i++)
            {
                if (i > 0)
                {
                    drpList.Items.RemoveAt(1);
                }
            }

            drpList.DataTextField = textField;
            drpList.DataValueField = valueField;
            drpList.DataSource = list;
            drpList.DataBind();
        }
        /// <summary>
        /// 绑定下拉框数据到一个EnumDescript属性标记的数组中
        /// </summary>
        /// <param name="drpList"></param>
        /// <param name="AppendDataBoundItems"></param>
        public static void SetDropDownDataSource<T>(DropDownList drpList, bool AppendDataBoundItems = true)
        {
            drpList.AppendDataBoundItems = AppendDataBoundItems;
            drpList.DataTextField = "EnumDisplayText";
            drpList.DataValueField = "EnumValue";
            drpList.DataSource = EnumDescription.GetList<T>();
            drpList.DataBind();
        }

        /// <summary>
        /// 设置下拉框选中项的默认选中值
        /// </summary>
        /// <param name="drpList"></param>
        /// <param name="defaultValue"></param>
        public static void SetDropDownDefaultValue(DropDownList drpList, object defaultValue)
        {
            drpList.ClearSelection();
            if (!string.IsNullOrEmpty(defaultValue.ToString()))
            {
                var listItem = drpList.Items.FindByValue(defaultValue.ToString());
                if (listItem != null)
                {
                    listItem.Selected = true;
                }
            }
        }

        /// <summary>
        /// 绑定复选框列表到一个泛型列表中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ctrl"></param>
        /// <param name="list"></param>
        /// <param name="textField"></param>
        /// <param name="valueField"></param>
        /// <param name="AppendDataBoundItems"></param>
        public static void SetCheckBoxList<T>(CheckBoxList ctrl, IList<T> list, string textField, string valueField, bool AppendDataBoundItems = true)
        {

            ctrl.AppendDataBoundItems = AppendDataBoundItems;
            var nCount = ctrl.Items.Count;
            for (int i = 0; i < nCount; i++)
            {
                if (i > 0)
                {
                    ctrl.Items.RemoveAt(1);
                }
            }

            ctrl.DataTextField = textField;
            ctrl.DataValueField = valueField;
            ctrl.DataSource = list;
            ctrl.DataBind();
        }
        /// <summary>
        /// 设置复选框列表的默认选中值
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="values"></param>
        public static void SetCheckBoxListDefaultValue(CheckBoxList ctrl, List<int> values )
        {
            if (values!=null && values.Count>0)
            {
                foreach (var value in values)
                {
                    var listItem = ctrl.Items.FindByValue(value.ToString());
                    if (listItem != null)
                    {
                        listItem.Selected = true;
                    }
                }
            }
        }

    }
}
